package dji.ux.ext.setting.aircraft;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProvider;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;

public class LimitAltitudePanel extends ConstraintLayoutWidget {
    private SchedulerProvider schedulerProvider;
    private EditText editText;
    private int mHeightLimit;
    private LimitAltitudeModel limitAltitudeModel;

    public LimitAltitudePanel(Context context) {
        super(context);
    }

    public LimitAltitudePanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LimitAltitudePanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_limit_altitude, this);
        schedulerProvider = SchedulerProvider.getInstance();
        editText = view.findViewById(R.id.et_limit_altitude);
        editText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {//点击软键盘完成控件时触发的行为
                //关闭光标并且关闭软键盘
                getEditTextDistanceLimit();
                setMaxFlightHeight();
                /*InputMethodManager im = (InputMethodManager) ContextUtil.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                im.hideSoftInputFromWindow(ContextUtil.getCurrentActivity().getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                int uiOptions = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
                ContextUtil.getCurrentActivity().getWindow().getDecorView().setSystemUiVisibility(uiOptions);*/
            }
            return true;//消费掉该行为
        });

        setClickable(true);
        setFocusable(true);

        if (!isInEditMode()) {
            limitAltitudeModel = new LimitAltitudeModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance(),
                    schedulerProvider);
        }

        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!isInEditMode()) {
            limitAltitudeModel.setup();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (!isInEditMode()) {
            limitAltitudeModel.cleanup();
        }
        super.onDetachedFromWindow();
    }

    private void getEditTextDistanceLimit() {
        final String s = editText.getText().toString().trim();
        if (s != null) {
            mHeightLimit = Integer.parseInt(s);
            if (5000 < mHeightLimit) {
                mHeightLimit = 5000;
            } else if (mHeightLimit < 15) {
                mHeightLimit = 15;
            }
        }
    }

    private void setMaxFlightHeight(){
        addDisposable(limitAltitudeModel.setMaxHeight(mHeightLimit).subscribe(() -> {
            // Do nothing
        }, logErrorConsumer(TAG, "switch distanceLimit mode: ")));
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(limitAltitudeModel.getAltitude()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::updateEditText));
    }

    private void updateEditText(float value) {
        editText.setText(String.valueOf(value));
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }
}
